/*
Copyright (c) (2012) (Anthony Bigot, Vincent Loppin)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions: The above copyright notice and
this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef VEC2_H
#define VEC2_H

#include <iostream>

namespace Util{

/*! \class Vec2<T>
  * \brief Manage vectors 2D
  * \author Anthony BIGOT & Vincent LOPPIN
  * \version 1.0
  *
  */
    template <class T>
    class Vec2 {
    public:
        Vec2()
            {
                v[0] = 0;		v[1] = 0;
            }

            Vec2(T _x, T _y)
            {
                v[0] = _x;
                v[1] = _y;
            }

            /// Vector add
            inline const Vec2<T> operator + (const Vec2<T> & vec) const{
                return Vec2<T>(	vec.v[0] + v[0],
                                vec.v[1] + v[1]);
            }

            /// Vector substract
            inline const Vec2<T> operator - (const Vec2<T> & vec) const{
                return Vec2<T>(	v[0] - vec.v[0],
                                v[1] - vec.v[1]);
            }

            /// Product with a scalar
            inline const Vec2<T> operator * (const T sca) const{
                return Vec2<T>(v[0]*sca,v[1]*sca);
            }

            /// Divide by a scalar
            inline const Vec2<T> operator / (const T sca) const{
                return Vec2<T>(v[0]/sca,v[1]/sca);
            }

            /// Dot product
            inline const T operator * (const Vec2<T> & vec) const{
                return v[0]*vec.v[0] + v[1]*vec.v[1];
            }

            inline T&	x()			{ return v[0]; }
            inline T	x() const	{ return v[0]; }
            inline T&	y()			{ return v[1]; }
            inline T	y() const	{ return v[1]; }

            inline Vec2<T> & operator= (const Vec2<T> & vec)
            {
                if(this == &vec) return *this;
                v[0] = vec.v[0];		v[1] = vec.v[1];
                return *this;
            }

            inline void print(std::ostream &o) const { o << "[" << v[0] << ";" << v[1] << "]"; }

            inline T * ptr(){ return (T*)v; }
            inline T * ptr() const{ return (T *)v; }
        protected:
            T v[2];
    };

    template<typename T>
    std::ostream & operator<< (std::ostream & o, const Vec2<T> & v)
    {
        o.precision(5);
        o << std::fixed;
        o << "[" << v.ptr()[0] << ";" << v.ptr()[1] << "]" << std::endl;
        return o;
    }

}

typedef Util::Vec2<double>        Vec2d;
typedef Util::Vec2<int>           Vec2i;
typedef Util::Vec2<unsigned int>  Vec2ui;
typedef Util::Vec2<float>         Vec2f;
typedef Util::Vec2<unsigned char> Vec2uc;


#endif // VEC2_H
